from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re

class CreatingFilm(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "http://localhost:8080/"
        self.verificationErrors = []
        self.accept_next_alert = True
    
    def test_create_film(self):
        driver = self.driver
        driver.get(self.base_url + "/php4dvd/")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("admin")
        driver.find_element_by_name("password").clear()
        driver.find_element_by_name("password").send_keys("admin")
        driver.find_element_by_name("submit").click()
        driver.find_element_by_css_selector("img[alt=\"Add movie\"]").click()
        driver.find_element_by_name("name").clear()
        driver.find_element_by_name("name").send_keys("The Shawshank Redemption")
        driver.find_element_by_name("year").clear()
        driver.find_element_by_name("year").send_keys("1994")
        driver.find_element_by_name("duration").clear()
        driver.find_element_by_name("duration").send_keys("142")
        driver.find_element_by_name("rating").clear()
        driver.find_element_by_name("rating").send_keys("9.3")
        driver.find_element_by_id("own_no").click()
        driver.find_element_by_name("trailer").clear()
        driver.find_element_by_name("trailer").send_keys("http://www.cinemagia.ro/trailer/the-shawshank-redemption-inchisoarea-ingerilor-1999/")
        driver.find_element_by_name("taglines").clear()
        driver.find_element_by_name("taglines").send_keys("Fear can hold you prisoner. Hope can set you free.")
        driver.find_element_by_name("plots").clear()
        driver.find_element_by_name("plots").send_keys("Andy Dufresne is a young and successful banker whose life changes drastically when he is convicted and sentenced to life imprisonment for the murder of his wife and her lover. Set in the 1940's, the film shows how Andy, with the help of his friend Red, the prison entrepreneur, turns out to be a most unconventional prisoner.")
        driver.find_element_by_id("text_languages_0").clear()
        driver.find_element_by_id("text_languages_0").send_keys("English")
        driver.find_element_by_name("country").clear()
        driver.find_element_by_name("country").send_keys("USA")
        driver.find_element_by_name("genres").clear()
        driver.find_element_by_name("genres").send_keys("Crime | Drama")
        driver.find_element_by_name("director").clear()
        driver.find_element_by_name("director").send_keys("Frank Darabont")
        driver.find_element_by_name("writer").clear()
        driver.find_element_by_name("writer").send_keys("Stephen King (short story \"Rita Hayworth and Shawshank Redemption\"), Frank Darabont (screenplay)")
        driver.find_element_by_name("cast").clear()
        driver.find_element_by_name("cast").send_keys("Tim Robbins, Morgan Freeman, Bob Gunton")
        driver.find_element_by_id("submit").click()
        driver.find_element_by_link_text("Log out").click()
        self.assertRegexpMatches(self.close_alert_and_get_its_text(), r"^Are you sure you want to log out[\s\S]$")
    
    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True
    
    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return text
        finally: self.accept_next_alert = True
    
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()